package sevensoft.创建型模式.单例模式.破坏单例模式;

import sevensoft.创建型模式.单例模式.饿汉式.Singleton;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class 序列化破坏方式 {
    public static final String FULL_NAME="D:\\序列化";

    public static void main(String[] args) throws NoSuchFieldException, IOException, ClassNotFoundException {
        oof();
        oif();
    }

    public static void oif() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(Paths.get(FULL_NAME)));
        Singleton singleton= (Singleton) objectInputStream.readObject();
        System.out.println(singleton);
        objectInputStream.close();
    }

    public static void oof() throws IOException, ClassNotFoundException {
        Singleton instance = Singleton.getInstance();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(Files.newOutputStream(Paths.get(FULL_NAME)));
        objectOutputStream.writeObject(instance);
        System.out.println(instance);
        objectOutputStream.close();
    }
}
